/**************************************************************************
 * @file usr_input_handle.c
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2025-10-21
 * 
 * @copyright Copyright (c) 2025
 * 
 *************************************************************************/
#include "hc32_ll_hrpwm.h"
#include "usr_display_handle.h"
#include "usr_input_handle.h"
#include "usr_uart.h"
#include "usr_ec11.h"
#include "usr_pd_i2c.h"

void disable_output_handle(char *dis_str) {
    main_dat.ps_out = PS_OUT_DISABLE;
    main_dat.ps_delay_to_close_hrpwm = 100u;
    // usr_set_output_vol(&main_dat.buck_boost, CFG_VO_MIN);
    // lcd_ascii_str_dis(2,64,ascii_16x16_font,dis_str);

    HRPWM_IDLE_EnterImmediate(HRPWM_BST_PWM_SYNC_UNIT, HRPWM_SW_SYNC_CH_ALL);
    HRPWM_IDLE_EnterImmediate(HRPWM_BUCK_PWM_SYNC_UNIT, HRPWM_SW_SYNC_CH_ALL);
}

RAM_FUNC void usr_btn_msg_cb_handle(mo_vk_code_t code, mo_btn_pattern_t btn_patt, u8 click_num) {
    DDL_Printf("k-msg:code:%d,bp:%d,ck-num:%d\n", code, btn_patt, click_num);
    switch (code) {
        case HW_KEY_CENTER:
            if (btn_patt == BTN_SINGLE_CLICK) {
                if (main_dat.ps_out == PS_OUT_DISABLE) {
                    main_dat.ps_out = PS_OUT_ENABLE;
                    CAP_DISCARGE_CTL(CAP_DISCARGE_OFF);
                    main_dat.soft_vo = 100.0f;  // mV
                    main_dat.soft_up_en = 1;
                    main_dat.soft_up_cnt = 0;
                    main_dat.buck_boost.wk_mode = BUCK_W_MODE;
                    buck_boost_data_init();

                    usr_set_output_vol(&main_dat.buck_boost, main_dat.soft_vo);
                    main_dat.ps_delay_to_close_hrpwm = 0u;
                    HRPWM_IDLE_Exit(HRPWM_BUCK_PWM_SYNC_UNIT, HRPWM_SW_SYNC_CH_ALL);
                    HRPWM_IDLE_Exit(HRPWM_BST_PWM_SYNC_UNIT, HRPWM_SW_SYNC_CH_ALL);
#if LCD_PANEL_ENABLE
                    lcd_ascii_str_dis(2, 64, ascii_16x16_font, "ON ");
#else
                    usr_uart_send_a_msg(OUTPUT_STATE_CMD, "CV");
#endif
                } else {
                    main_dat.ps_out = PS_OUT_DISABLE;
                    main_dat.ps_delay_to_close_hrpwm = 100u;
                    // // usr_set_output_vol(&main_dat.buck_boost, CFG_VO_MIN);
                    // lcd_ascii_str_dis(2,64,ascii_16x16_font,"OFF ");
                    HRPWM_IDLE_EnterImmediate(HRPWM_BST_PWM_SYNC_UNIT, HRPWM_SW_SYNC_CH_ALL);
                    HRPWM_IDLE_EnterImmediate(HRPWM_BUCK_PWM_SYNC_UNIT, HRPWM_SW_SYNC_CH_ALL);

#if LCD_PANEL_ENABLE
                    disable_output_handle("OFF ");
#else
                    usr_uart_send_a_msg(OUTPUT_STATE_CMD, "OFF");
#endif
                }
            } else if (btn_patt == BTN_MULTI_CLICK && click_num == 2) {
                main_dat.cfg_state++;
                main_dat.cfg_state %= CFG_MENU_MAX;
                // usr_cfg_item_display(main_dat.cfg_state, main_dat.usr_cfg.cfg_vo, main_dat.usr_cfg.cfg_io);
                usr_cfg_item_active_handle(main_dat.cfg_state);

            } else if (btn_patt == BTN_MULTI_CLICK && click_num == 4) {
            } else if (btn_patt == BTN_MULTI_CLICK && click_num == 3) {
                static USB_INPUT_CFG usb_in = USB_IN_CFG_5V;
                usb_in++;
                usb_in %= USB_IN_CFG_MAX_;
                usr_PD_I2C_Write(2, &usb_in, 1);
                // uint8_t test_b[20];
                // for(int i=0;i<20;i++){
                //     test_b[i]=0xa5;
                // }
                // BSP_24CXX_Write(0, test_b, sizeof(test_b));
                DDL_Printf("PD Cfg input:%d\n", usb_in);

            } else if (btn_patt == BTN_LP) {
            } else if (btn_patt == BTN_MULTI_CLICK && click_num == 5) {
            } else if (btn_patt == BTN_VLP) {
            }
            break;
        default:
            break;
    }
}

RAM_FUNC void usr_cfg_item_handle(uint8_t inc_flg, int32_t change_step) {
    int32_t *cfg_item;
    int32_t cfg_item_max_value, cfg_item_min_value;

    switch (main_dat.cfg_state) {
        case CFG_VOLTAGE_ACTIVE:
            cfg_item = &main_dat.usr_cfg.cfg_vo;
            cfg_item_max_value = CFG_VO_MAX;
            cfg_item_min_value = CFG_VO_MIN;
            // change_step = 100u;
            break;
        case CFG_CURRENT_ACTIVE:
            cfg_item = &main_dat.usr_cfg.cfg_io;
            cfg_item_max_value = CFG_IO_MAX;
            cfg_item_min_value = CFG_IO_MIN;
            // change_step = 100u;
            break;
        case CFG_MENU_NULL:
        default:
            return;
            break;
    }

    if (inc_flg) {  // inc
        *cfg_item += change_step;
        if (*cfg_item > cfg_item_max_value) *cfg_item = cfg_item_max_value;
    } else {  // dec
        *cfg_item -= change_step;
        if (*cfg_item < cfg_item_min_value) *cfg_item = cfg_item_min_value;
    }

    main_dat.usr_cfg_save_timeout = CFG_SAVE_TIMEOUT;
    usr_cfg_item_display(main_dat.cfg_state, main_dat.usr_cfg.cfg_vo, main_dat.usr_cfg.cfg_io);
}

int32_t adj_step = 10;
uint16_t adj_interval = 0;
RAM_FUNC void rotate_encoder_handle(void) {
    /********************旋转编码器处理********************************** */
    EC11_EVENT_T ec11 = get_ec11_roate_state();
    if (ec11 == EC11_ROTATE_CCW) { /*逆时针旋转*/
        // DDL_Printf("EC11_ROTATE_CCW\n");
        usr_cfg_item_handle(0, adj_step);
        adj_interval = ROTATE_TRIG_TIMEOUT;
        if(main_dat.cfg_state == CFG_VOLTAGE_ACTIVE){
            adj_step = 1000u;
        }else{
            adj_step = 500u;
        }
        
    } else if (ec11 == EC11_ROTATE_CW) { /*顺时针旋转*/
        // DDL_Printf("EC11_ROTATE_CW\n");
        usr_cfg_item_handle(1, adj_step);
        adj_interval = ROTATE_TRIG_TIMEOUT;
        adj_step = 1000u;
    } else {
    }

    if (adj_interval) {
        adj_interval--;
        if (adj_interval < ROTATE_TIMEOUT_L3) {
            if (main_dat.cfg_state == CFG_VOLTAGE_ACTIVE) {
                adj_step = 10u;
            } else {
                adj_step = 10u;
            }
        } else if (adj_interval < ROTATE_TIMEOUT_L2) {
            if (main_dat.cfg_state == CFG_VOLTAGE_ACTIVE) {
                adj_step = 50u;
            } else {
                adj_step = 100u;
            }
        } else if (adj_interval < ROTATE_TIMEOUT_L1) {
            if (main_dat.cfg_state == CFG_VOLTAGE_ACTIVE) {
                adj_step = 100u;
            } else {
                adj_step = 200u;
            }
        } else {
        }
    }
    /************************************************** */
}